<?php
/**
 * 卡券核销
 * Class CouponAfterScan
 */
namespace Scan;
use Model\AfterCouponMod;
use Model\AfterPersonMod;
use Model\CouponSnapshotMod;
use Model\StorePositionMod;
use Model\StoreStaffMod;
use Pub\cache\Cache;

class CouponAfterScan extends Scan
{
    private $CouponSnapshot = null;
    private $AfterCoupon = null;  //门店核销人员
    private $AfterPerson = null;  //活动核销人员

    private $_scan_key = '';  //核销二维码key
    private $_check_time = '';  //核销二维码时间戳
    private $_snapshot_id = 0;  //获得快照id
    private $_member_treasure = array();  //获得卡券记录数据
    public function __construct()
    {
        parent::__construct();
    }

    //1、验证key
    //2、验证扫码的用户是否有和核销权限
    //3、进行核销改变数据

    public function index()
    {
        if(!$this->checkParam()) return $this->_result;
        if(!$this->checkKey()) return $this->_result;
        if(!$this->getMemberTreasure()){
            $this->_result['err_msg'] = '核销数据不存在';
            return $this->_result;
        }
        //验证卡券数量
        if($this->_member_treasure['use_number'] >= $this->_member_treasure['number']){
            $this->_result['err_msg'] = '错误，没有可以核销的卡券了';
            return $this->_result;
        }

        //验证卡券是否过期
        if($this->_member_treasure['past_time'] != 1){
            if(($this->_member_treasure['past_time']+86400) < TIMESTAMP){
                $this->_result['err_msg'] = '卡券已过期';
                return $this->_result;
            }
        }


        //验证员工
        if(!$this->checkAfterStaff()){
            $this->_result['err_msg'] = '您没有核销权限';
            return $this->_result;
        }

        //核销操作
        if(!$this->CouponSnapshot->math(array('use_number +'=>1),array('id'=>$this->_snapshot_id))){
            $this->_result['err_msg'] = '核销失败，请重试';
            return $this->_result;
        }
        Cache::remove(Cache::USER_COUPON_AFTER_KEY,array('uid'=>$this->_dispose_uid));

        //添加核销记录
        $this->afterCouponRecord();

        $this->_result = array('status'=>'success','success_msg'=>'卡券核销成功');
        return $this->_result;
    }

    /**
     * 验证所需参数
     */
    private function checkParam()
    {
        global $_GPC;
        $_result = true;
        if(!isset($_GPC['snapshot_id']) || !isset($_GPC['uid']) || !isset($_GPC['scan_key']) || !isset($_GPC['check_time'])){
            $this->_result['err_msg'] = '非法操作';
            $_result = false;
        }
        $this->_snapshot_id = $_GPC['snapshot_id'];
        $this->_scan_key = $_GPC['scan_key'];
        $this->_check_time = $_GPC['check_time'];
        return $_result;
    }

    /**
     * 验证key
     */
    private function checkKey()
    {
        $_result = false;
        $_scan_key = Cache::get(Cache::USER_COUPON_AFTER_KEY,array('uid'=>$this->_dispose_uid));
        if($_scan_key && $_scan_key['expire'] > TIMESTAMP){
            if(($this->_scan_key == $_scan_key['key'])){
                $_salt = sha1($this->_snapshot_id.$this->_dispose_uid.$this->_check_time);
                if($_salt != $this->_scan_key || $_salt != $_scan_key['key']){
                    $this->_result['err_msg']= '核销码验证失败';
                }else{
                    $_result = true;
                }
            }else{
                $this->_result['err_msg']= '核销码验证失败';
            }
        }else{
            $this->_result['err_msg']= '核销码失效';
        }
        return $_result;
    }

    /**
     * 获得卡券的记录数据
     * @return mixed
     */
    private function getMemberTreasure()
    {

        $this->CouponSnapshot = new CouponSnapshotMod();
        $this->_member_treasure = $this->CouponSnapshot->findOne(array('id'=>$this->_snapshot_id),array('activity_id','use_number','number','past_time','store_id'));
        return $this->_member_treasure;
    }


    /**
     * 验证当前用户是不是核销人员
     */
    private function checkAfterStaff()
    {
        if($this->_member_treasure['store_id'] == 0){
            //验证活动后台是不是核销人员
            return $this->checkActivityAfterStaff();
        }else{
            //验证门店核销店员
            return $this->checkStoreAfterStaff();
        }
    }

    /**
     * 验证当前用户是不是（门店的）核销人员
     */
    private function checkActivityAfterStaff()
    {
        $this->AfterPerson = new AfterPersonMod();
        return $this->AfterPerson->findOne(array('activity_id'=>$this->_member_treasure['activity_id'],'uid'=>$this->_scan_uid,'status'=>1),array('id'));
    }

    /**
     * 验证门店核销店员
     */
    private function checkStoreAfterStaff()
    {
        $StoreStaff = new StoreStaffMod();
        $_store_staff = $StoreStaff->findOne(array('store_id'=>$this->_member_treasure['store_id'],'uid'=>$this->_scan_uid),array('position_id'));
        if($_store_staff){
            if($_store_staff['position_id'] == 0) return true;  //店长直接扫码
            $StorePosition = new StorePositionMod();
            $_store_position = $StorePosition->findOne(array('id'=>$_store_staff['position_id']),array('authority'));
            $_authority = json_decode($_store_position['authority'],true);
            if(in_array('verify',$_authority)){
                return true;
            }
        }
        return false;
    }

    /**
     * 核销卡券记录
     */
    private function afterCouponRecord()
    {
        $this->AfterCoupon = new AfterCouponMod();
        $this->AfterCoupon->insert(array(
            'store_id'=>$this->_member_treasure['store_id'],
            'staff_id'=>$this->_scan_uid,
            'uid'=>$this->_dispose_uid,
            'time'=>TIMESTAMP
        ));
    }
}
